//
//  CapturePhotoCollectionViewCell.m
//  Rail
//
//  Created by hychen on 2018/5/1.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "CapturePhotoCollectionViewCell.h"

@interface CapturePhotoCollectionViewCell ()
@property (nonatomic, strong) NSMutableArray *selectImageArray;
/***  背景图片按钮*/
@property (nonatomic, weak) UIButton *backButton;
/***  打钩按钮*/
@property (nonatomic, weak) UIButton *deleteButton;

@end
@implementation CapturePhotoCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _MaxPhotoesCount = 3;
        [self setupSubviews];
    }
    return self;
}

// 子控件
- (void)setupSubviews
{
    [self setupBackButton];
    
    [self setupDeleteButton];
}

- (void)setupBackButton
{
    __weak typeof (self) weakSelf = self;
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.layer.cornerRadius = 5;
    backButton.layer.masksToBounds = YES;
    backButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
    backButton.contentMode = UIViewContentModeScaleAspectFill;
    [backButton addTarget:self action:@selector(backButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:backButton];
    self.backButton = backButton;
    [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(weakSelf.contentView);
        make.right.equalTo(weakSelf.contentView).offset(-5);
        make.top.equalTo(weakSelf.contentView).offset(5);
    }];
}

- (void)setupDeleteButton
{
    __weak typeof (self) weakSelf = self;
    UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [deleteButton setImage:[UIImage imageNamed:@"select_user_delete"] forState:UIControlStateNormal];
    [deleteButton addTarget:self action:@selector(deleteButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:deleteButton];
    self.deleteButton = deleteButton;
    [deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(SCREEN_WIDTH/375*16);
        make.right.top.equalTo(weakSelf.contentView);
    }];
}

// 删除
- (void)deleteButtonDidClicked:(UIButton *)button
{
    if (self.BlockClickDeletePhoto) {
        self.BlockClickDeletePhoto(self.indexPath);
    }
}

// 抓取图片
- (void)backButtonDidClicked:(UIButton *)button
{
    if (self.BlockClickPhoto) {
        self.BlockClickPhoto(self.model);
    }
}

- (void)setModel:(CapturePhotoModel *)model {
    _model = model;
    if (model) {
        [self.backButton setImage:model.image forState:UIControlStateNormal];
        if (model.isAddPhoto) {
            self.deleteButton.hidden = YES;
            self.backButton.layer.cornerRadius = 0;
            self.backButton.layer.masksToBounds = NO;
        } else {
            self.deleteButton.hidden = NO;
            self.backButton.layer.cornerRadius = 5;
            self.backButton.layer.masksToBounds = YES;
        }
    }
}

// 调整选择照片尺寸
- (void)resizeButtonWithSmall:(BOOL)isSmall
{
    __weak typeof (self) weakSelf = self;
    if (isSmall) {
        [self.backButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentView).offset(25);
            make.top.equalTo(weakSelf.contentView).offset(35);
        }];
    } else {
        [self.backButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentView).offset(-5);
            make.top.equalTo(weakSelf.contentView).offset(5);
        }];
    }
    [self.contentView layoutIfNeeded];
}

@end
